import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Index from '@/views/Index.vue'
import Order from '@/components/Order.vue'
import User from '@/components/User.vue'
import Statistics from '@/components/Statistics.vue'
import Warehouse from '@/components/Warehouse.vue'
import orderdata from '@/components/Order/orderdata.vue'
import purchasedata from '@/components/Order/purchasedata.vue'
import userdata from '@/components/User/userdata.vue'
import staffdata from '@/components/User/staffdata.vue'
import providerdata from '@/components/Warehouse/providerdata.vue'
import storehouse from '@/components/Warehouse/storehouse.vue'
import repository from '@/components/Warehouse/repository.vue'
import waterdata from '@/components/Warehouse/waterdata.vue'
import commodity from '@/components/Statistics/commodity.vue'
import income from '@/components/Statistics/income.vue'
import compare from '@/components/Statistics/compare.vue'

const routes = [
  {
    path: '/',
    name: "login",
    component: Login
  },
  {
    path: '/Index',
    name: "Index",
    component: Index,
    children: [
      {
        path: 'Order', component: Order, alias: '', children:
          [
            { path: 'orderdata', component: orderdata, alias: '' },
            { path: 'purchasedata', component: purchasedata },
          ]
      },
      {
        path: 'User', component: User, children:
          [
            { path: 'userdata', component: userdata, alias: '' },
            { path: 'staffdata', component: staffdata },

          ]
      },
      {
        path: 'Statistics', component: Statistics, children: [
          {path: 'commodity', component: commodity, alias: ''},
          {path: 'income', component: income},
          {path:'compare',component:compare},
        ]
      },
      {
        path: 'Warehouse', component: Warehouse, children:
          [
            { path: 'providerdata', component: providerdata, alias: '' },
            { path: 'repository', component: repository },
            { path: 'storehouse', component: storehouse },
            { path: 'waterdata', component: waterdata },
          ]
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name == 'login') {
    next();
    return;
  }
  let name=sessionStorage.getItem('sid');

  if(name==undefined&&to.path!='login'){
     next('/');
  }
  next();
})

export default router
